<!--
 * @Author: weidewei
 * @Date: 2025-11-03 09:14:35
 * @LastEditors: weidewei
 * @LastEditTime: 2025-11-03 09:58:49
 * @Description: 
 * @FilePath: \2025-front-face\review-knowledge\20251102\复习手写instanceof关键字.html
-->
<script>
  function myInstanceOf(instance,constructorFn) {
    // 1. 检查 Constructor 是否为有效构造函数（有 prototype 属性）或对象/函数
    if(typeof constructorFn !== 'function' && !(constructorFn && typeof constructorFn === 'object' && 'prototype' in constructorFn)) return false;
    // 2. 检查 instance 是否为原始值（原始值无原型链，直接返回 false）
    if(instance === null || (typeof instance !== 'object' && typeof instance !== 'function')) return false;
    let proto = Object.getPrototypeOf(instance);
    // 3. 循环遍历原型链，直到找到 Constructor.prototype 或到达链顶端（null）
    while(proto) {
        if(proto === constructorFn.prototype) {
            return true
        }
        proto = Object.getPrototypeOf(proto);
    }
    return false;
  }

  function Person(name, age) {
    console.log("this", this);
    this.name = name;
    this.age = age;
  }

  const p = new Person('张三',20);
//   console.log('是否是实例',p instanceof Person);
  console.log('是否是实例',myInstanceOf(null,Person));
</script>
